# -------- builder --------
FROM golang:1.22 AS builder
WORKDIR /src

# 仅复制依赖声明，加快缓存
COPY go.mod go.sum ./
RUN go mod download

# 再复制源码
COPY . .

# 生产编译（禁用 CGO，静态链接）
ARG BIN_NAME=k8s_operation
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
    go build -trimpath -ldflags="-s -w" -o /out/${BIN_NAME} ./cmd/server

# -------- runtime --------
# 你也可以换成 gcr.io/distroless/static:nonroot
FROM alpine:3.20

# 非 root 账号（安全）
RUN addgroup -S app && adduser -S app -G app
USER app

WORKDIR /app
# 用于日志落盘的目录（与你的配置 storage/logs 对齐）
RUN mkdir -p /app/storage/logs

# 拷贝二进制
COPY --from=builder /out/k8s_operation /app/k8s_operation
# 拷贝默认配置（如果你容器里直接跑）
COPY configs/config.yaml /app/configs/config.yaml

ENV GIN_MODE=release
ENV APP_CONFIG=/app/configs/config.yaml

EXPOSE 8080
ENTRYPOINT ["/app/k8s_operation"]
